package com.fh.Thread_Exercise;
/*
（1）在main方法中启动两个线程
（2）第1个线程循环随机打印100以内的整数
（3）直到第2个线程从键盘读取了“Q”命令。
 */



import java.util.Random;
import java.util.Scanner;

public class Exercise02 {
    public static void main(String[] args) {
        C1 c1 = new C1();
        C2 c2 = new C2(c1);
        c1.start();
        new Thread(c2).start();

    }
}
class C1 extends Thread {
    private boolean loop = true;
    @Override
    public void run() {
        Random random = new Random();
        while (loop) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            int num = random.nextInt(101);
            System.out.println(num);
        }
        System.out.println("C1线程退出...Q");
    }
    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}
class C2 implements Runnable {
   private C1 c1;
   public C2(C1 c1) {
       this.c1 = c1;
   }
    @Override
    public void run() {
        Scanner myscanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你的指令（Q）表示退出...");
                char target = myscanner.next().toUpperCase().charAt(0);
                if (target == 'Q') {
                    c1.setLoop(false);
                    System.out.println("C2线程退出...");
                    break;
                }
            }
        }
}
